/*
 * Filename:    AnalyseController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月16日
 */
package com.paic.mhis.hcpms.evaluation.evaluationRule.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.evaluation.evaluationRule.dto.EvaluationCategoryDTO;
import com.paic.mhis.hcpms.evaluation.evaluationRule.dto.EvaluationMarkValueDTO;
import com.paic.mhis.hcpms.evaluation.evaluationRule.dto.EvaluationStandardScoreContentDTO;
import com.paic.mhis.hcpms.evaluation.evaluationRule.dto.EvaluationTypeDTO;
import com.paic.mhis.hcpms.evaluation.evaluationRule.intergration.dao.IEvaluationRuleService;
import com.paic.mhis.hcpms.evaluation.evaluationRule.web.vo.MarkRuleInfoVO;
import com.paic.mock.util.StringUtils;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

@Controller
@RequestMapping("/evaluationRule/")
public class EvaluationRuleController {

	@BizServicesProxy(beanName = "evaluationRuleServiceImpl")
	private IEvaluationRuleService evaluationRuleService;

	/**
	 * 保存扣分设置
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("saveMarkRuleValue.do")
	@ResponseBody
	public Map<String, Object> saveMarkRuleValue(HttpServletRequest request, HttpServletResponse response) {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String markRuleId = request.getParameter("markRuleId");
		String[] list = request.getParameterValues("list");

		List<EvaluationMarkValueDTO> currentList = new ArrayList<EvaluationMarkValueDTO>();

		if (list != null && list.length > 0) {

			for (String value : list) {
				JSONObject transferJson = JSON.parseObject(value);

				MarkRuleInfoVO vo = JSONObject.toJavaObject(transferJson, MarkRuleInfoVO.class);

				EvaluationMarkValueDTO bean = new EvaluationMarkValueDTO();
				bean.setCreateUser(user.getUsername());
				bean.setUpdateUser(user.getUsername());
				bean.setMarkRuleId(markRuleId);
				bean.setIsValid(vo.getIsValid());
				bean.setMarkValueName(vo.getScoreName());
				bean.setMarkValueScore(vo.getUpperValues());

				currentList.add(bean);

			}

		}

		this.evaluationRuleService.processMarkRuleInfo(markRuleId, currentList, user);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		return returnMap;
	}

	/**
	 * 加载考评规则信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("loadEvaluationRules.do")
	@ResponseBody
	public Map<String, Object> loadEvaluationRules(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> parameterMap = new HashMap<String, Object>();

		String categoryType = request.getParameter("categoryType");
		String id = request.getParameter("id");

		parameterMap.put("categoryType", categoryType);
		if (StringUtils.hasText(id)) {
			parameterMap.put("id", id);
		}
		List<EvaluationCategoryDTO> list = this.evaluationRuleService.loadEvaluationRules(parameterMap);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("list", list);
		return returnMap;
	}

	/**
	 * 删除考评规则信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("deleteEvaluationRules.do")
	@ResponseBody
	public Map<String, Object> deleteEvaluationRules(HttpServletRequest request, HttpServletResponse response) {

		String categoryType = request.getParameter("categoryType");
		String[] categoryList = request.getParameterValues("list");

		this.evaluationRuleService.deleteEvaluationCategory(categoryList, categoryType);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("msg", "操作成功了!");
		return returnMap;
	}

	/**
	 * 初始化考评类别的数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("initEvaluationTypeData.do")
	@ResponseBody
	public Map<String, Object> initEvaluationTypeData(HttpServletRequest request, HttpServletResponse response) {

		String scoreType = request.getParameter("scoreType");

		List<EvaluationTypeDTO> list = this.evaluationRuleService.loadAllEvaluationType(scoreType);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("list", list);
		return returnMap;
	}

	/**
	 * 初始化考评类别的数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("initEvaluationContentData.do")
	@ResponseBody
	public Map<String, Object> initEvaluationContentData(HttpServletRequest request, HttpServletResponse response) {

		String scoreType = request.getParameter("scoreType");
		String standardTypeId = request.getParameter("standardTypeId");

		List<EvaluationStandardScoreContentDTO> list = this.evaluationRuleService.loadAllTreeContent(standardTypeId,
				scoreType);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		returnMap.put("list", list);
		return returnMap;
	}

	/**
	 * 保存考评规则信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping("saveEvaluationRuleData.do")
	@ResponseBody
	public Map<String, Object> saveEvaluationRuleData(HttpServletRequest request, HttpServletResponse response)
			throws BusinessException {

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String str = null;
		try {
			str = HttpProcessUtil.readRequestBody(request);
		} catch (UnsupportedEncodingException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		} catch (IOException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		}
		String businessType = "";
		JSONObject transferJson = JSON.parseObject(str);
		String name = transferJson.get("name").toString();
		Object businessTypeObject = transferJson.get("businessType");
		if (null != businessTypeObject) {
			businessType = businessTypeObject.toString();
		}
		String evaluationYear = transferJson.get("evaluationYear").toString();
		String catetoryType = transferJson.get("catetoryType").toString();
		JSONArray arrayList = transferJson.getJSONArray("list");

		EvaluationCategoryDTO ruleBean = new EvaluationCategoryDTO();

		ruleBean.setCategoryName(name);
		ruleBean.setRuleType(catetoryType);

		if (StringUtils.hasText(businessType)) {
			ruleBean.setBusinessType(businessType);
		}

		ruleBean.setEvaluationYear(evaluationYear);

		List<EvaluationStandardScoreContentDTO> currentList = new ArrayList<EvaluationStandardScoreContentDTO>();

		if (arrayList != null && arrayList.size() > 0) {

			for (Object value : arrayList) {

				JSONObject currentJson = JSON.parseObject(value.toString());
				EvaluationStandardScoreContentDTO vo = JSONObject.toJavaObject(currentJson,
						EvaluationStandardScoreContentDTO.class);

				currentList.add(vo);

			}

		}
		this.evaluationRuleService.saveEvaluationRuleInfo(currentList, ruleBean, user);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		return returnMap;
	}

	/**
	 * 更新考评规则
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping("updateEvaluationRuleData.do")
	@ResponseBody
	public Map<String, Object> updateEvaluationRuleData(HttpServletRequest request, HttpServletResponse response)
			throws BusinessException {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		String str = null;
		try {
			str = HttpProcessUtil.readRequestBody(request);
		} catch (UnsupportedEncodingException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		} catch (IOException e) {
			throw new BusinessException("获取参数出错!请联系管理员");
		}

		JSONObject transferJson = JSON.parseObject(str);
		String name = transferJson.get("name").toString();

		String businessType = "";
		Object businessTypeObject = transferJson.get("businessType");
		if (null != businessTypeObject) {
			businessType = businessTypeObject.toString();
		}

		String evaluationYear = transferJson.get("evaluationYear").toString();
		String catetoryType = transferJson.get("catetoryType").toString();
		String ruleId = transferJson.get("ruleId").toString();
		JSONArray arrayList = transferJson.getJSONArray("list");

		EvaluationCategoryDTO ruleBean = new EvaluationCategoryDTO();

		ruleBean.setCategoryName(name);
		ruleBean.setRuleType(catetoryType);
		ruleBean.setId(ruleId);

		if (StringUtils.hasText(businessType)) {
			ruleBean.setBusinessType(businessType);
		}
		ruleBean.setEvaluationYear(evaluationYear);

		List<EvaluationStandardScoreContentDTO> currentList = new ArrayList<EvaluationStandardScoreContentDTO>();

		if (arrayList != null && arrayList.size() > 0) {

			for (Object value : arrayList) {
				JSONObject currentObject = JSON.parseObject(value.toString());

				EvaluationStandardScoreContentDTO vo = JSONObject.toJavaObject(currentObject,
						EvaluationStandardScoreContentDTO.class);

				currentList.add(vo);

			}

		}
		this.evaluationRuleService.updateEvaluationRuleInfo(currentList, ruleBean, user);

		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("flag", true);
		return returnMap;
	}

}
